package com.tsu.mall.admin.util;


import com.tsu.mall.admin.entity.Attendant;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//请求工具类
public class RequestUtil {
    //将保存在Session中的对象起的名字作为常量值提取出来
    private static final String CURRENT="currentAttendant";
   // 调用此静态方法直接返回这次请求的request对象
    public static HttpServletRequest getRequest(){
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }
    //返回此次请求的Session对象
    public  static HttpSession getSession(){
        return getRequest().getSession();
    }
    //保存当前用户进Session
    public static void current(Attendant attendant){
        getSession().setAttribute(CURRENT,attendant);
    }
    //获取当前Session中保存的登录对象
    public static Attendant getCurrent(){
        Object obj=getSession().getAttribute(CURRENT);
        if (obj==null){
            return null;
        }
        return (Attendant) obj;
    }

}
